// Generated on 2014-05-12 using generator-angular 0.8.0


// # Globbing
// for performance reasons we're only matching one level down:
// 'test/spec/{,*/}*.js'
// use this if you want to recursively match all subfolders:
// 'test/spec/**/*.js'

module.exports = function (grunt) {

    require('load-grunt-tasks')(grunt);

    // Time how long tasks take. Can help when optimizing build times
    require('time-grunt')(grunt);

    var pathBuildOnDemand = ":../../../build_widgets.json";

    // Define the configuration for all the tasks
    grunt.initConfig({

        // Project settings
        yeoman: {
            // configurable paths
            app: require('./bower.json').appPath || 'app',
            dist: 'dist/'
        },


        // Replace url of image in dist/main.css file.
        replace: {
            css: {
                src: ['<%= yeoman.dist %>/styles/*.css'],
                dest: '<%= yeoman.dist %>/styles/',
                replacements: [
                    {
                        from: '/bower_components/Font-Awesome/fonts/',
                        to: 'fonts/'
                    },
                    {
                        from: '/bower_components/font-awesome/fonts/',
                        to: 'fonts/'
                    },
                    {
                        from: '/components/',
                        to: '../images/'
                    },
                    {
                        from: '/bower_components/bootstrap/dist/fonts/',
                        to: 'fonts/'
                    }

                ]
            },
            html: {
                src: ['<%= yeoman.dist %>/index.html'],
                dest: '<%= yeoman.dist %>/index.html',
                replacements: [
                    {
                        from: 'components/',
                        to: 'images/'
                    }
                ]
            }
        },

        //create app/app/template.js file. Contain all template files.
        ngtemplates: {
            app: {
                cwd: '<%= yeoman.app %>/components/',
                src: ['**/*-tpl.html', '**/template/*.html', '**/templates/*.html'],
                dest: '<%= yeoman.app %>/components/template.js',
                options: {
                    module: "appSkeleton",
                    url: function (url) {
                        var urlSp = url.split("/");
                        var p1 = urlSp.pop();
                        var p2 = urlSp.pop();
                        if (p2 == 'template' || p2 == 'templates')
                            return p2 + '/' + p1;
                        else
                            return p1;
                    },
                    htmlmin: {
                        collapseBooleanAttributes: true,
                        collapseWhitespace: true,
                        removeAttributeQuotes: true,
                        removeComments: true, // Only if you don't use comment directives!
                        removeEmptyAttributes: true,
                        removeRedundantAttributes: true,
                        removeScriptTypeAttributes: true,
                        removeStyleLinkTypeAttributes: true
                    }
                }
            }
        },

        shell: {
            templateWidgets: {
                command: [
                    'cd app/bower_components/xcomponent-widgets',
                    'grunt templateCache'
                ].join('&&')
            },
            compassWidgets: {
                command: [
                    'cd app/bower_components/xcomponent-widgets',
                    'grunt compass:dist'

                ].join('&&')
            },
            buildWidgets: {
                command: [
                    'cd app/bower_components/xcomponent-widgets',
                    'npm install',
                    'grunt buildOnDemand' + pathBuildOnDemand
                ].join('&&')
            },
            bowerInstall: {
                command: [
                    'bower install --allow-root',
                    'grunt wiredep'
                ].join('&&')
            }

        },
        //ng-docs for create javaScript docs folder
        ngdocs: {

            options: {
                scripts: [
                    'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false',
                    '.tmp/concat/scripts/vendor.js',
                    '.tmp/concat/scripts/scripts.js'
                ],

                styles: []
            },
            api: {
                src: ['app/components/**/*.js', 'app/services/**/*.js',
                    'app/bower_components/xcomponent/**/*.js',
                    'app/bower_components/xcomponent-widgets/**/*.js'],
                title: 'API Documentation'
            }
        },

        // Watches files for changes and runs tasks based on the changed files
        watch: {
//            bower: {
//                files: ['bower.json'],
//                tasks: ['bowerInstall']
//            },
//            js: {
//                files: ['<%= yeoman.app %>/components/**/*.js', '<%= yeoman.app %>/services/**/*.js'],
//                tasks: [],
//                options: {
//                    livereload: true
//                }
//            }
            html: {
                files: ['<%= yeoman.app %>/components/**/*.html'],
                tasks: ['ngtemplates']

            },

            templateWidgets: {
                files: ['<%= yeoman.app%>/bower_components/xcomponent-widgets/**/*.html'],
                tasks: ['shell:templateWidgets']
            },

            compassWidgets: {
                files: ['<%= yeoman.app%>/bower_components/xcomponent-widgets/**/*.scss'],
                tasks: ['shell:compassWidgets']
            }

//            bowerInstall: {
//                files: ['<%= yeoman.app%>/**/.bower.json'],
//                tasks: [ 'bowerInstall']
//            }
            , componentSCSS: {
                files: ['<%= yeoman.app%>/components/**/*.scss'],
                tasks: ['compass:dev']
            }
        },


        // Make sure code styles are up to par and there are no obvious mistakes
        jshint: {
            options: {
                "node": true,
                "browser": true,
                "esnext": true,
                "bitwise": true,
                "camelcase": true,
                "curly": true,
                "eqeqeq": true,
                "immed": true,
                "indent": 2,
                "latedef": true,
                "newcap": true,
                "noarg": true,
                "quotmark": "single",
                "regexp": true,
                "undef": true,
                "unused": true,
                "strict": true,
                "trailing": true,
                "smarttabs": true,
                "globals": {
                    "angular": false
                },
                reporter: require('jshint-stylish')
            },
            all: [
                'gruntfile.js',
                '<%= yeoman.app %>/components/**/*.js',
                '<%= yeoman.app %>/services/**/*.js'
            ],
            test: {
                options: {
                    jshintrc: 'test/.jshintrc'
                },
                src: ['test/spec/{,*/}*.js']
            }
        },

        // Empties folders to start fresh
        clean: {
            options: {
                force: true
            },
            dist: {
                files: [
                    {
                        dot: true,
                        src: [
                            '.tmp',
                            '<%= yeoman.app %>/components/**/*.css',
                            '<%= yeoman.app %>/components/template.js',
                            '<%= yeoman.dist %>'
                        ]
                    }
                ]
            },
            server: '.tmp',
            docs: 'docs',
            tmp: '.tmp',
            dev: {

                files: [
                    {
                        dot: true,
                        src: [
                            '.tmp',
                            'docs',
                            '.*',
                            '<%= yeoman.app %>/bower_components',
                            '<%= yeoman.dist %>',
                            '!<%= yeoman.dist %>/.git*',
                            '<%= yeoman.app %>/components/**/*.css',
                            '<%= yeoman.app %>/components/template.js'
                        ]
                    }
                ]
            }
        },

        // Add vendor prefixed styles

        // autoprefixer: {
        // options: {
        // browsers: ['last 1 version']
        // },
        // dist: {
        // files: [
        // {
        // expand: true,
        // cwd: '.tmp/styles/',
        // src: '{,*/}*.css',
        // dest: '.tmp/styles/'
        // }
        // ]
        // }
        // },

        // Automatically inject Bower components into the app
        wiredep: {
            app: {
                src: ['<%= yeoman.app %>/index.html'],
                ignorePath: '<%= yeoman.app %>/'
            },
            sass: {
                src: ['<%= yeoman.app %>/styles/{,*/}*.{scss,sass}'],
                ignorePath: '<%= yeoman.app %>/bower_components/'
            }
        },

        // Compiles Sass to CSS and generates necessary files if requested
        compass: {

            options: {
                sassDir: '<%= yeoman.app %>/components',
                cssDir: ['.tmp/styles', '<%= yeoman.app %>/components'],
                relativeAssets: false,
                httpPath: "."
            },
            dist: {
                options: {
                    environment: 'production',
                    imagesDir: '../assets'
                }
            },
            dev: {
                options: {
                    environment: 'production',
                    imagesDir: '<%= yeoman.app %>/components/assets',
                    relativeAssets: true
                }
            },
            server: {
                options: {
                    environment: 'development',
                    imagesDir: '<%= yeoman.app %>/components/assets'

                }
            }
        },

        // Renames files for browser caching purposes
        rev: {
            dist: {
                files: {
                    src: [
                        '<%= yeoman.dist %>/scripts/{,*/}*.js',
                        '<%= yeoman.dist %>/styles/{,*/}*.css'
//                        '<%= yeoman.dist %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}',
//                        '<%= yeoman.dist %>/styles/fonts/*'
                    ]
                }
            }
        },

        // Reads HTML for usemin blocks to enable smart builds that automatically
        // concat, minify and revision files. Creates configurations in memory so
        // additional tasks can operate on them
        useminPrepare: {
            html: '<%= yeoman.app %>/index.html'

        },

        // Performs rewrites based on rev and the useminPrepare configuration
        usemin: {
            html: ['<%= yeoman.dist %>/index.html']

        },



        imagemin: {
            options: {
                force: true
            },
            dist: {
                files: [
                    {
                        expand: true,
                        cwd: '<%= yeoman.app %>/components',
                        src: '**/*.{png,jpg,jpeg,gif}',
                        dest: '<%= yeoman.dist %>'
                    }
                ]
            }
        },

        htmlmin: {
            dist: {
                options: {
                    collapseWhitespace: true,
                    collapseBooleanAttributes: true,
                    removeCommentsFromCDATA: true,
                    removeOptionalTags: true
                },
                files: [
                    {
                        expand: true,
                        cwd: '<%= yeoman.dist %>',
                        src: ['*.html', 'views/{,*/}*.html'],
                        dest: '<%= yeoman.dist %>'
                    }
                ]
            }
        },

        // ngmin tries to make the code safe for minification automatically by
        // using the Angular long form for dependency injection. It doesn't work on
        // things like resolve or inject so those have to be done manually.
        ngAnnotate: {
            dist: {
                files: [
                    {
                        expand: true,
                        src: '.tmp/**/*.js',

                    }
                ]
            }
        },


        // Copies remaining files to places other tasks can use
        copy: {
            dist: {
                files: [
                    {
                        expand: true,
                        dot: true,
                        cwd: '<%= yeoman.app %>',
                        dest: '<%= yeoman.dist %>',
                        src: [
                            '*.{ico,png,txt}',
                            '.htaccess',
                            '*.html',
                            'views/{,*/}*.html',
                            'images/{,*/}*.{webp}',
                            'fonts/*'
                        ]
                    },
                    {
                        expand: true,
                        cwd: '.tmp/images',
                        dest: '<%= yeoman.dist %>/images',
                        src: ['generated/*']
                    }
                    ,
                    {
                        expand: true,
                        cwd: '<%= yeoman.app %>/bower_components/font-awesome',
                        dest: '<%= yeoman.dist %>/',
                        src: ['fonts/*.*']
                    },
                    {
                        expand: true,
                        cwd: '<%= yeoman.app %>/bower_components/xcomponent-widgets/components/object-display-storyline/TimelineJS/build/css',
                        dest: '<%= yeoman.dist %>/styles',
                        src: ['**']
                    }  ,
                    {
                        expand: true,
                        cwd: '<%= yeoman.app %>/bower_components/xcomponent-widgets/components/object-display-storyline/TimelineJS/build/',
                        dest: '<%= yeoman.dist %>/',
                        src: ['js/locale/**']
                    }
                    ,
                    {
                        expand: true,
                        cwd: '<%= yeoman.app %>/components/themes',
                        dest: '<%= yeoman.dist %>/components/themes',
                        src: ['**']
                    },
                    {
                        expand: true,
                        cwd: '<%= yeoman.app %>/bower_components/bootswatch/',
                        dest: '<%= yeoman.dist %>/bower_components/bootswatch/',
                        src: ['**/*min.css', 'fonts/*.*']
                    },
                    {
                        expand: true,
                        cwd: '<%= yeoman.app %>/bower_components/xcomponent-widgets/target/dist/pdfViewer/',
                        dest: '<%= yeoman.dist %>/bower_components/pdfViewer/',
                        src: ['**/*.*']
                    },
                    {
                        expand: true,
                        cwd: '<%= yeoman.app %>/',
                        dest: '<%= yeoman.dist %>/',
                        src: ['*.pdf']
                    }
                ]
            },

            html:{
                expand: true,
                flatten: true,
                filter: 'isFile',
                cwd:"<%= yeoman.app%>/",
                src:"index.html",
                dest:"<%= yeoman.dist%>/"

            }
        },

        // By default, your `index.html`'s <!-- Usemin block --> will take care of
        // minification. These next options are pre-configured if you do not wish
        // to use the Usemin blocks.

        concurrent: {
            minimize: ['cssmin', 'uglify']
        },


        grunt: {
            buildWidgets: {
                gruntfile: 'app/bower_components/xcomponent-widgets/Gruntfile.js',
                task
                    :
                    'buildOnDemand'+pathBuildOnDemand
            }
        },

        filerev: {
            assets: {
                src: [
                    '<%= yeoman.dist%>/**/*.css',
                    '<%= yeoman.dist%>/**/*.js',
                    '!<%= yeoman.dist%>/**/pdfViewer/**/*.*',
                    '!<%= yeoman.dist%>/**/locale/**/*.*'
                ]

            }
        }


    });


    grunt.registerTask('build', [
        'clean:dist',       // Remove all of css, template.css
        'shell:bowerInstall',     //Install bower
        'grunt:buildWidgets',//Build widget
        'compass',
        'ngtemplates',      //Generate templateCache
        'useminPrepare',
        'concat:generated',
        'ngAnnotate',
        'cssmin:generated',
        'uglify:generated',
        'copy',
        'filerev',
        'usemin'
    ]);




};
